import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;

public class MapScreen extends Screen
{
	private ArrayList<Hotspot> busHotspots;
	private ArrayList<Hotspot> areaHotspots;
	private Screen bus1Screen, bus2Screen, bus3Screen, bus4Screen;
	private Screen routeMenuScreen;
	private Screen bus1MapScreen, bus2MapScreen, bus3MapScreen, bus4MapScreen;
	private Screen areaOverlay;
	private Screen zoom1Screen, zoom2Screen;
	private Screen currBusScreen, currMapScreen, currPopupScreen;
	private Screen popupScreen1, popupScreen2;
	private Screen bus1;
	private RouteScreen routeScreen1, routeScreen2;
	private boolean zoomed;
	private boolean popup;
	private SmoothMoves animation;

	
	public MapScreen(SmoothMoves _animation)
	{
		// Call parent
		super("imgs/top_s1.png");
		
		// Set animation handle
		animation = _animation;
		
		// Initialize things
		busHotspots = new ArrayList<Hotspot>();
		areaHotspots = new ArrayList<Hotspot>();
		zoomed = false;
		popup = false;

		// Load subscreens
		bus1Screen = new Screen("imgs/map_menu2_s1.png");
		bus2Screen = new Screen("imgs/map_menu2_s2.png");
		bus3Screen = new Screen("imgs/map_menu2_s3.png");
		bus4Screen = new Screen("imgs/map_menu2_s4.png");
		bus1MapScreen = new Screen("imgs/map_screen_s1.png");
		bus2MapScreen = new Screen("imgs/map_screen_s2.png");
		bus3MapScreen = new Screen("imgs/map_screen_s3.png");
		bus4MapScreen = new Screen("imgs/map_screen_s4.png");
		areaOverlay = new Screen("imgs/area_selection.png");
		popupScreen1 = new Screen("imgs/pop_window.png");
		popupScreen2 = new Screen("imgs/pop_window.png");
		zoom1Screen = new Screen("imgs/map_route_screen_s1.png");
		zoom2Screen = new Screen("imgs/map_route_screen_s2.png");
		routeMenuScreen = new Screen("imgs/map_route_menu2_s1.png");
		routeScreen1 = new RouteScreen("imgs/route_screen_s1.png");
		routeScreen2 = new RouteScreen("imgs/route_screen_s2.png");
		bus1 = new Screen("imgs/bus_icon_s1.png");
		
		
		// Set subscreen positions		
		bus1Screen.setPosition(129, 88);
		bus2Screen.setPosition(129, 88);
		bus3Screen.setPosition(129, 88);
		bus4Screen.setPosition(129, 88);
		bus1MapScreen.setPosition(222, 88);
		bus2MapScreen.setPosition(222, 88);
		bus3MapScreen.setPosition(222, 88);
		bus4MapScreen.setPosition(222, 88);
		zoom1Screen.setPosition(222, 88);
		zoom2Screen.setPosition(222, 88);
		areaOverlay.setPosition(269, 95);
		routeMenuScreen.setPosition(129, 88);
		popupScreen1.setPosition(382,214);
		popupScreen2.setPosition(382,214);
		routeScreen1.setPosition(222, 88);
		routeScreen2.setPosition(222, 88);
	
		
		// Sidebar hotspots
		busHotspots.add(new Hotspot(135, 198, 233, 295, bus1Screen));
		busHotspots.add(new Hotspot(135, 317, 233, 414, bus2Screen));
		busHotspots.add(new Hotspot(135, 438, 233, 535, bus3Screen));
		busHotspots.add(new Hotspot(135, 92, 233, 180, bus4Screen));
		routeMenuScreen.addHotspot(new Hotspot(135, 135, 235, 235, bus1Screen));
		
		// Area hotspots
		areaHotspots.add(new Hotspot(469, 112, 469 + 120, 112 + 115, zoom1Screen));
		areaHotspots.add(new Hotspot(668, 364, 668 + 125, 364 + 100, zoom2Screen));
		
		// Zoom screen hotspots
		zoom1Screen.addHotspot(new Hotspot(402, 233, 432, 263, popupScreen1));
		zoom1Screen.addHotspot(new Hotspot(475, 289, 505, 319, popupScreen1));
		zoom1Screen.addHotspot(new Hotspot(650, 504, 680, 534, popupScreen1));// right one
		zoom1Screen.addHotspot(new Hotspot(782, 647, 812, 677, popupScreen1));
		zoom1Screen.addHotspot(new Hotspot(937, 655, 967, 685, popupScreen1));
		zoom2Screen.addHotspot(new Hotspot(495, 259, 525, 289, popupScreen2));// right one
		zoom2Screen.addHotspot(new Hotspot(497, 603, 527, 633, popupScreen2));
		zoom2Screen.addHotspot(new Hotspot(768, 510, 798, 540, popupScreen2));
		zoom2Screen.addHotspot(new Hotspot(813, 338, 843, 368, popupScreen2));
		zoom2Screen.addHotspot(new Hotspot(842, 284, 872, 314, popupScreen2));

		// Popup screen hotspots
		popupScreen1.addHotspot(new Hotspot(381, 481, 381 + 244, 481 + 84, null));  //cancel 
		popupScreen1.addHotspot(new Hotspot(381 + 244, 481, 381 + 244*2, 481 + 84, routeScreen1));  //proceed
		popupScreen2.addHotspot(new Hotspot(381, 481, 381 + 244, 481 + 84, null));
		popupScreen2.addHotspot(new Hotspot(381 + 244, 481, 381 + 244*2, 481 + 84, routeScreen2));

		// Set initial subscreens
		currBusScreen = bus4Screen;
		currMapScreen = bus4MapScreen;
		currPopupScreen = null;
	
	}

	/// This method is called when a search query is entered
	public void doSearch(String query)
	{
		System.out.println("Searching: " + query);
		zoomed = true;
		currMapScreen = routeScreen1;
		currBusScreen = routeMenuScreen;
	}
	@Override
	public void draw(Graphics g)
	{
		g.drawImage(bg, 129, 0, null);
		currMapScreen.draw(g);
		currBusScreen.draw(g);
		// Draw overlay unless zoomed in
		if (!zoomed)
		{
			//areaOverlay.draw(g);
		}
		if (currPopupScreen != null)
		{
			currPopupScreen.draw(g);
		}
	}
	
	public void searchToMap()
	{
		animation.setRoute(0);
		currMapScreen = zoom1Screen;
		currBusScreen = routeMenuScreen;
		zoomed = true;
	}
	
	@Override
	public void checkClick(int x, int y)
	{
		if (x < 130)
		{
			// Outside map screen, reset
			currBusScreen = bus4Screen;
			currMapScreen = bus4MapScreen;
			currPopupScreen = null;
			zoomed = false;
			popup = false;
		}
		if (!zoomed)
		{
			// Check against area hotspots
			for (int i=0; i<areaHotspots.size(); ++i)
			{
				Hotspot h = areaHotspots.get(i);
				if (h.contains(x, y))
				{
					currMapScreen = h.screen;
					currBusScreen = routeMenuScreen;
					zoomed = true;
					animation.setRoute(i);
				}
			}
	
			// Check against bus hotspots
			for (int i=0; i<busHotspots.size(); ++i)
			{
				Hotspot h = busHotspots.get(i);
				if (h.contains(x, y))
				{
					switch(i)
					{
					case 0:
						currMapScreen = bus1MapScreen;
						break;
					case 1:
						currMapScreen = bus2MapScreen;
						break;
					case 2:
						currMapScreen = bus3MapScreen;
						break;
					case 3:
						currMapScreen = bus4MapScreen;
						break;
					}
					currBusScreen = h.screen;
				}
			}
		}
		else
		{
			if (popup)
			{
				// Check against popup hotspots
				if (currPopupScreen.getHotspots().get(0).contains(x, y))
				{
					// Clickced cancel
					popup = false;
					currPopupScreen = null;
				
				}
				else if (currPopupScreen.getHotspots().get(1).contains(x, y))
				{
					// Clickced proceed
					popup = false;
					currMapScreen = currPopupScreen.getHotspots().get(1).screen;
					currPopupScreen = null;
					animation.restart();
					animation.setActive(true);
				}
			}
			else
			{
				// We're zoomed in
				if (routeMenuScreen.getHotspots().get(0).contains(x, y))
				{
					animation.setActive(false);
					zoomed = false;
					currBusScreen = bus4Screen;
					currMapScreen = bus4MapScreen;
				}
				
				// Check against station hotspots
				ArrayList<Hotspot> list = currMapScreen.getHotspots();
				for (int i=0; i<list.size(); ++i)
				{
					Hotspot h = list.get(i);
					if (h.contains(x, y))
					{
						currPopupScreen = h.screen;
						popup = true;
					}
				}
			}
		}
	}
}